package com.example.yls.demoa.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.example.yls.demoa.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yls on 2018/6/14.
 */

public class SingleCustomView extends View {
    private static int MAX_COUNT=10;
    private float w;
    private Paint paint;
    private List<Point> whith=new ArrayList<>();
    private List<Point> black=new ArrayList<>();
    private boolean isWhith=true;
    private Bitmap b;
    private Bitmap wh;
    private static int count=1;
    private static int READ_LEFT_OR_RIGHT=5;
    private boolean winner;
    private int l=1;
    private int r=1;

    public SingleCustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint=new Paint();

        //setAntiAlias方法为抗锯齿，true为开
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        setBackgroundColor(Color.parseColor("#8B4513"));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w=w;

        //初始化Bitmap，如果放在init中执行，会出错，应为view一创建后就会调用构造方法，在调用时，onSizeChange方法尚未执行，此时w值为0；这时在使用w去除其他数值，会出现错误。
        Bitmap b1=BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
        b=Bitmap.createScaledBitmap(b1,(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),false);
        wh=Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2),(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTable(canvas);
        drawQizi(canvas);
    }

    /**
     * 绘制棋子的方法
     * @param canvas
     */
    private void drawQizi(Canvas canvas) {
        //判断是否比赛结束
        if (checkFinish()){
            Toast.makeText(getContext(),"比赛结束",Toast.LENGTH_SHORT).show();
        }

        //遍历黑白棋数组，获取其point的值，并利用point的x，y来绘制棋子位置
        for (int i=0;i<black.size();i++){
            canvas.drawBitmap(b,(w/MAX_COUNT)/4+(w/MAX_COUNT)*black.get(i).x,(w/MAX_COUNT)/4+(w/MAX_COUNT)*black.get(i).y,paint);
        }
        for (int i=0;i<whith.size();i++){
            canvas.drawBitmap(wh,(w/MAX_COUNT)/4+(w/MAX_COUNT)*whith.get(i).x,(w/MAX_COUNT)/4+(w/MAX_COUNT)*whith.get(i).y,paint);
        }
    }


    /**
     * 判断过比赛是否结束的方法
     * 棋子数量大于5时才会进行检测
     * 检测时以最后一个棋子为准进行检测
     */
    private boolean checkFinish() {
        //********************************************白旗检测******************************************
        r=1;
        //白旗横向检测
        if (whith.size()>=5){
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x-(i+1),whith.get(whith.size()-1).y))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x+(i+1),whith.get(whith.size()-1).y))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }

        //白旗纵向检测
        if (whith.size()>=5){
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x,whith.get(whith.size()-1).y-(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x,whith.get(whith.size()-1).y+(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }

        //白旗斜向检测
        if (whith.size()>=5){
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x-(i+1),whith.get(whith.size()-1).y-(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x+(i+1),whith.get(whith.size()-1).y+(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }
        if (whith.size()>=5){
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x-(i+1),whith.get(whith.size()-1).y+(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (whith.contains(new Point(whith.get(whith.size()-1).x+(i+1),whith.get(whith.size()-1).y-(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }

        //************************************黑棋检测****************************************************
        if (black.size()>=5){
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x-(i+1),black.get(black.size()-1).y))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x+(i+1),black.get(black.size()-1).y))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }

        //白旗纵向检测
        if (black.size()>=5){
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x,black.get(black.size()-1).y-(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x,black.get(black.size()-1).y+(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }

        //白旗斜向检测
        if (black.size()>=5){
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x-(i+1),black.get(black.size()-1).y-(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x+(i+1),black.get(black.size()-1).y+(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }
        if (black.size()>=5){
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x-(i+1),black.get(black.size()-1).y+(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            for (int i=0;i<5;i++){
                if (black.contains(new Point(black.get(black.size()-1).x+(i+1),black.get(black.size()-1).y-(i+1)))){
                    r++;
                }else {
                    break;
                }
            }
            if (r==5){
                winner=true;
                return true;
            }
            r=1;
        }
        return false;
    }


    /**
     * 绘制棋盘
     * @param canvas
     */
    public void drawTable(Canvas canvas){
        for (int i=0;i<MAX_COUNT;i++){
            float startX=(w/MAX_COUNT)/2+(w/MAX_COUNT)*i;
            float startY=(w/MAX_COUNT)/2+(w/MAX_COUNT)*i;
            float endX=w-(w/MAX_COUNT)/2;
            float endY=w-(w/MAX_COUNT)/2;
            canvas.drawLine((w/MAX_COUNT)/2,startY,endX,startY,paint);
            canvas.drawLine(startX,(w/MAX_COUNT)/2,startX,endY,paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                //判断点击的位置是否存在棋子，point的比较方式是通过x,y值来比较的，不是比较地址的。
                if (whith.contains(new Point((int)(event.getX()/(w/10)),(int)(event.getY()/(w/10))))){
                    Log.e("白色拦截","拦截成功");
                    return false;
                }else if (black.contains(new Point((int)(event.getX()/(w/10)),(int)(event.getY()/(w/10))))){
                    Log.e("白色拦截","拦截成功");
                    return false;
                }
                if (winner){
                    Toast.makeText(getContext(),"比赛结束",Toast.LENGTH_SHORT).show();
                    return false;
                }

                //checkWhith(x,y)方法是将点击位置的x，y坐标计算成棋子在棋盘中的相对位置，如0，1则为第一行第二个棋子
                if (isWhith){
                    whith.add(checkWhith(event.getX(),event.getY()));
                }else {
                    black.add(checkWhith(event.getX(),event.getY()));
                }
                invalidate();
                isWhith=!isWhith;
                break;
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width=MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width,width);
    }

    public Point checkWhith(float x,float y){
        int mx=(int)(x/(w/10));
        int my=(int)(y/(w/10));
        return new Point(mx,my);
    }
}
